<?php

namespace api\modules\v1\forms\clinic;

use Yii;
use yii\base\Model;
use common\models\member\Member;
use common\models\clinic\Clinic;

/**
 * 诊室
 *
 * Class ClinicForm
 * @package api\modules\v1\forms\clinic
 */
class ClinicForm extends Model
{
    /**
     * 诊室ID
     *
     * @var
     */
    public $id;

    /**
     * 诊室名
     *
     * @var
     */
    public $name;

    /**
     * 项目ID
     *
     * @var
     */
    public $project_id;

    /**
     * 诊室状态
     *
     * @var
     */
    public $status;

    /**
     * 排序
     *
     * @var
     */
    public $sort;

    /**
     * 诊室信息
     *
     * @var array
     */
    protected $clinic;


    public function rules()
    {
        return [
            // 参数验证
            [['project_id', 'name', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['project_id', 'id', 'name', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['project_id', 'id'], 'required', 'message' => '{attribute}不能为空', 'on' => ['delete']],
            [['project_id', 'id', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit_status']],

            [['project_id'], 'checkProjectId', 'skipOnEmpty' => false, 'on' => ['create', 'edit', 'delete', 'edit_status']],
            [['id'], 'checkClinic', 'skipOnEmpty' => false, 'on' => ['edit', 'delete', 'edit_status']],
            [['name'], 'checkName', 'skipOnEmpty' => false, 'on' => ['create', 'edit']],
            [['status'], 'checkStatus', 'skipOnEmpty' => false, 'on' => ['create', 'edit', 'edit_status']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['project_id', 'name', 'status', 'sort'];

        // 编辑
        $scenarios['edit'] = ['project_id', 'id', 'name', 'status', 'sort'];

        // 删除
        $scenarios['delete'] = ['project_id', 'id'];

        // 切换部门状态
        $scenarios['edit_status'] = ['project_id', 'id', 'status'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => '诊室ID',
            'name' => '诊室名称',
            'project_id' => '项目ID',
            'status' => '状态',
            'sort' => '排序',
        ];
    }

    /**
     * 检验项目ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkProjectId($attribute)
    {
        if (Yii::$app->user->identity->member->is_super == Member::IS_SUPER_YES) {
            return true;
        }

        $attr = Yii::$app->services->attr->findInfo([
            'member_id' => Yii::$app->user->identity->member_id,
            'project_id' => $this->project_id,
        ]);

        if (!$attr) {
            $this->addError($attribute, '未绑定当前项目，无法操作');
            return true;
        }
    }

    /**
     * 检验诊室信息
     *
     * @param $attribute
     * @return bool
     */
    public function checkClinic($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $clinic = $this->getClinic();
        if (!$clinic) {
            $this->addError($attribute, '参数错误[id_error]');
            return true;
        }

        if ($clinic->status == Clinic::STATUS_DELETE) {
            $this->addError($attribute, '诊室已删除，无法操作');
            return true;
        }

        if (($this->getScenario() == 'edit' && $this->status == Clinic::STATUS_INVALID)
            || $this->getScenario() == 'delete'
            || ($this->getScenario() == 'edit_status' && $this->status == Clinic::STATUS_INVALID)
        ) {
            $attr = Yii::$app->services->attr->findInfo(['clinic_id' => $this->id]);

            if ($attr) {
                $this->addError($attribute, "该诊室有人员绑定，无法操作");
                return true;
            }
        }
    }

    /**
     * 校验诊室名称
     *
     * @param $attribute
     */
    public function checkName($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $len = mb_strlen($this->name);
        if ($len > 20) {
            $this->addError($attribute, '诊室名称过长，最多20个字');
            return true;
        }

        // 是否检验
        $isCheck = true;
        if ($this->getScenario() == 'edit') {
            $clinic = $this->getClinic();
            // 诊室名称未变【跳过】
            if ($clinic->name == $this->name) {
                $isCheck = false;
            }
        }

        if ($isCheck) {
            $clinicInfo = Yii::$app->services->clinic->findInfo([
                'project_id' => $this->project_id,
                'name' => $this->name,
                'status' => [Clinic::STATUS_INVALID, Clinic::STATUS_ENABLE],
            ]);

            if ($clinicInfo) {
                $this->addError($attribute, "诊室【{$this->name}】已存在");
                return true;
            }
        }
    }

    /**
     * 校验诊室状态
     *
     * @param $attribute
     */
    public function checkStatus($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->status, [Clinic::STATUS_INVALID, Clinic::STATUS_ENABLE])) {
            $this->addError($attribute, "参数错误[status_error]");
            return true;
        }
    }

    /**
     * 获取诊室信息
     *
     * @return array|Clinic|null
     */
    public function getClinic()
    {
        if (empty($this->clinic)) {
            $this->clinic = Yii::$app->services->clinic->findById($this->id);
        }

        return $this->clinic;
    }
}